/**
 * 
 */
package com.cqan.entity.account;

import java.util.List;

import javax.persistence.*;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.cqan.entity.pk.IdEntity;

/**
 * @author slaton
 *
 * 下午03:10:14
 */
@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "SYS_ORG")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Organization extends IdEntity {

	private String name;
	
	private String ename;
	
	private Integer status;
	
	private List<User> users;
	
	private List<Role> roles;
	
	private Organization parent;
	
	private List<Organization> children;

	@OneToMany(mappedBy = "parent", cascade = { CascadeType.REFRESH}, fetch = FetchType.LAZY, targetEntity = Organization.class)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Organization> getChildren() {
		return children;
	}

	
	public String getEname() {
		return ename;
	}

	
	public String getName() {
		return name;
	}

	@ManyToOne
	@JoinColumn(name="pid")
	public Organization getParent() {
		return parent;
	}

	//多对多定义
	@ManyToMany
	//中间表定义,表名采用默认命名规则
	@JoinTable(name = "SYS_ORG_ROLE", joinColumns = { @JoinColumn(name = "orgid") }, inverseJoinColumns = { @JoinColumn(name = "rid") })
	//Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	//集合按id排序.
	@OrderBy("id")
	//集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoles() {
		return roles;
	}

	
	public Integer getStatus() {
		return status;
	}

	@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "orgs",fetch = FetchType.LAZY)
	public List<User> getUsers() {
		return users;
	}

	
	public void setChildren(List<Organization> children) {
		this.children = children;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	
	public void setName(String name) {
		this.name = name;
	}

	
	public void setParent(Organization parent) {
		this.parent = parent;
	}

	
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	
	public void setStatus(Integer status) {
		this.status = status;
	}

	
	public void setUsers(List<User> users) {
		this.users = users;
	}
	
	
}
